package com.fxkj.cloud.drms.domain.product;

import com.fxkj.cloud.dbs.common.model.attribute.AttributeConstants;
import com.fxkj.cloud.dbs.common.model.kv.KvEntry;
import com.google.common.util.concurrent.ListenableFuture;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/4/18 13:21
 */
@Data
public class ProductEnergyStatistics {
    //今日能耗
    Float todayEnergy;
    //今日额定能耗
    Float todayRatedEnergy;
    //今日节能率
    Float todayEnergyConservationRate;
    // 昨日能耗
    Float yesterdayEnergy;
    //昨日额定能耗
    Float yesterdayRatedEnergy;
    //昨日节能率
    Float yesterdayEnergyConservationRate;
    // 本月能耗
    Float cmEnergy;
    //本月额定能耗
    Float cmRatedEnergy;
    //本月节能率
    Float cmEnergyConservationRate;
    // 上月能耗
    Float lmEnergy;
    //上月额定能耗
    Float lmRatedEnergy;
    //上月节能率
    Float lmEnergyConservationRate;
    // 总能耗
    Float totalEnergy;
    //总额定能耗
    Float totalRatedEnergy;
    //总节能率
    Float totalEnergyConservationRate;

    public ProductEnergyStatistics() {
        this.todayEnergy = 0F;
        this.todayRatedEnergy = 0F;
        this.todayEnergyConservationRate = 0F;
        this.yesterdayEnergy = 0F;
        this.yesterdayRatedEnergy = 0F;
        this.yesterdayEnergyConservationRate = 0F;
        this.cmEnergy = 0F;
        this.cmRatedEnergy = 0F;
        this.cmEnergyConservationRate = 0F;
        this.lmEnergy = 0F;
        this.lmRatedEnergy = 0F;
        this.lmEnergyConservationRate = 0F;
        this.totalEnergy = 0F;
        this.totalRatedEnergy = 0F;
        this.totalEnergyConservationRate = 0F;
    }

    public void statistics(
            Float todayEnergy, Float todayRatedEnergy,
            Float yesterdayEnergy, Float yesterdayRatedEnergy,
            Float cmEnergy, Float cmRatedEnergy,
            Float lmEnergy, Float lmRatedEnergy,
            Float totalEnergy, Float totalRatedEnergy) {
        this.todayEnergy += todayEnergy;
        this.todayRatedEnergy += todayRatedEnergy;
        this.yesterdayEnergy += yesterdayEnergy;
        this.yesterdayRatedEnergy += yesterdayRatedEnergy;
        this.cmEnergy += cmEnergy;
        this.cmRatedEnergy += cmRatedEnergy;
        this.lmEnergy += lmEnergy;
        this.lmRatedEnergy += lmRatedEnergy;
        this.totalEnergy += totalEnergy;
        this.totalRatedEnergy += totalRatedEnergy;
    }

    public static ProductEnergyStatistics statistics(Map<UUID, Float> deviceEnergy, Map<UUID, Map<String, KvEntry>> deviceLastValueMap) {
        ProductEnergyStatistics productEnergyStatistics = new ProductEnergyStatistics();
        deviceLastValueMap.forEach((deviceId, kvEntries) -> {
            productEnergyStatistics.statistics(
                    Float.valueOf(String.valueOf(deviceLastValueMap.get(deviceId).get(AttributeConstants.Service.E.Code.CDE.getFullCode()).getValueAsString())),
                    deviceEnergy.get(deviceId),
                    Float.valueOf(String.valueOf(deviceLastValueMap.get(deviceId).get(AttributeConstants.Service.E.Code.LDE.getFullCode()).getValueAsString())),
                    deviceEnergy.get(deviceId),
                    Float.valueOf(String.valueOf(deviceLastValueMap.get(deviceId).get(AttributeConstants.Service.E.Code.CME.getFullCode()).getValueAsString())),
                    deviceEnergy.get(deviceId) * Float.parseFloat(String.valueOf(deviceLastValueMap.get(deviceId).get(AttributeConstants.Service.S.Code.CMH.getFullCode()).getValueAsString())),
                    Float.valueOf(String.valueOf(deviceLastValueMap.get(deviceId).get(AttributeConstants.Service.E.Code.LME.getFullCode()).getValueAsString())),
                    deviceEnergy.get(deviceId) * Float.parseFloat(String.valueOf(deviceLastValueMap.get(deviceId).get(AttributeConstants.Service.S.Code.LMH.getFullCode()).getValueAsString())),
                    Float.valueOf(String.valueOf(deviceLastValueMap.get(deviceId).get(AttributeConstants.Service.E.Code.E.getFullCode()).getValueAsString())),
                    deviceEnergy.get(deviceId) * Float.parseFloat(String.valueOf(deviceLastValueMap.get(deviceId).get(AttributeConstants.Service.S.Code.WH.getFullCode()).getValueAsString()))
            );
        });
        productEnergyStatistics.calculateEnergyConservationRate();
        return productEnergyStatistics;
    }

    public void calculateEnergyConservationRate() {
        this.todayEnergyConservationRate = null == this.todayEnergy || this.todayEnergy.equals(0F) ?
                0F : 1 - (this.todayEnergy / (this.todayRatedEnergy));
        if (this.todayEnergyConservationRate < 0) {
            this.todayEnergyConservationRate = 0F;
        }
        this.yesterdayEnergyConservationRate = null == this.yesterdayRatedEnergy || this.yesterdayEnergy.equals(0F) ?
                0F : 1 - (this.yesterdayEnergy / (this.yesterdayRatedEnergy));
        if (this.yesterdayEnergyConservationRate < 0) {
            this.yesterdayEnergyConservationRate = 0F;
        }
        this.cmEnergyConservationRate = null == this.cmRatedEnergy || this.cmEnergy.equals(0F) ?
                0F : 1 - (this.cmEnergy / (this.cmRatedEnergy));
        if (this.cmEnergyConservationRate < 0) {
            this.cmEnergyConservationRate = 0F;
        }
        this.lmEnergyConservationRate = null == this.lmRatedEnergy || this.lmEnergy.equals(0F) ?
                0F : 1 - (this.lmEnergy / (this.lmRatedEnergy));
        if (this.lmEnergyConservationRate < 0) {
            this.lmEnergyConservationRate = 0F;
        }
        this.totalEnergyConservationRate = null == this.totalRatedEnergy || this.totalEnergy.equals(0F) ?
                0F : 1 - (this.totalEnergy / (this.totalRatedEnergy));
        if (this.totalEnergyConservationRate < 0) {
            this.totalEnergyConservationRate = 0F;
        }
    }


}
